//
//  SetLockViewController.m
//  QRMaster
//
//  Created by myApple on 15/5/5.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "SetLockViewController.h"
#import "SetLockView.h"
#import "SetCardViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import "AppDelegate.h"
#import "PromptInfoView.h"

@interface SetLockViewController ()<SetLockViewDelegate>
{
    UIImageView * promtView;
    float brightValue;
    UIImageView * changeTypeImageView;
    UILabel * typeLabel;
    SetLockView * slv;
}

@end

static SystemSoundID shake_sound_male_id = 0;

@implementation SetLockViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBA(237, 238, 241, 1);
    self.title = NSLocalizedString(@"setLock", nil);
    [self setLockView];
    [self prepareNavBtn];
//    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
//    if ([[userInfo objectForKey:@"setlock"]isEqualToString:@"yes"]) {
//        [self preparePromtView];
//    }
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hiddenKeyboard) name:@"KCHiddenKeyBoardNotification" object:nil];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    brightValue = [UIScreen mainScreen].brightness;
    changeTypeImageView.hidden = NO;
    [[UIScreen mainScreen] setBrightness:1.0];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [UIScreen mainScreen].brightness = brightValue;
    changeTypeImageView.hidden = YES;
    [[NSNotificationCenter defaultCenter]postNotificationName:@"moveWindown" object:nil];
}

-(void)prepareNavBtn
{
    changeTypeImageView = [[UIImageView alloc]initWithFrame:CGRectMake(UI_SCREEN_WIDTH - 96, 2, 80, 40)];
    changeTypeImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer * submitTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeType)];
    [changeTypeImageView addGestureRecognizer:submitTap];
    [self.navigationController.navigationBar addSubview:changeTypeImageView];
    
    typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 80, 20)];
    typeLabel.text = NSLocalizedString(@"auoth", nil);
    typeLabel.textColor = UIColorFromRGBAndA(0x7dc234,1.0);
    typeLabel.font = [UIFont systemFontOfSize:15.0];
    typeLabel.textAlignment = NSTextAlignmentRight;
    [changeTypeImageView addSubview:typeLabel];
}

#pragma mark ---------------- 切换类型
-(void)changeType
{
    [slv changeAuthorType];
}

-(BOOL)canBecomeFirstResponder
{
    return YES;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

#pragma mark  ---- 注册声音到系统
-(void)playSound
{
    NSString * path = [[NSBundle mainBundle]pathForResource:@"5018" ofType:@"wav"];
    if (path) {
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &shake_sound_male_id);
        AudioServicesPlayAlertSound(shake_sound_male_id);
    }
}


-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    [self playSound];
}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"shared" object:nil];
}


-(void)setLockView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = self.view.frame.size.height;
    float x = 0;
    float y = 0;
    float width = ScreenWidth;
    float height = ScreenHeight;
    
    slv = [[SetLockView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    slv.communityId = _communityId;
    slv.delegate = self;
    slv.backgroundColor = RGBA(237, 238, 241, 1);
    [self.view addSubview:slv];
}

#pragma mark ----------- setlockViewDelegate
-(void)nowAuthorType:(NSString *)typeName
{
    typeLabel.text = typeName;
}

-(void)noRoomInCommunity
{
    changeTypeImageView.hidden = YES;
}

-(void)preparePromtView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 0;
    float width = ScreenWidth;
    float height = ScreenHeight;
    
    promtView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    promtView.backgroundColor = RGBA(0, 0, 0, 0.5);
    promtView.userInteractionEnabled = YES;
    [self.view addSubview:promtView];
    UITapGestureRecognizer * promtViewClick = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickPromtView)];
    [promtView addGestureRecognizer:promtViewClick];
    
    
    x = 0;
    width = ScreenWidth;
    height = ScreenWidth / 2.0;
    y = ScreenHeight - height - 70;
    if (UI_SCREEN_HEIGHT == 480) {
        y = ScreenHeight - height - 60;
    }
    UIImageView * promtImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    promtImgView.image = [UIImage imageNamed:NSLocalizedString(@"switchCardStyle", nil)];
    [promtView addSubview:promtImgView];

}

-(void)clickPromtView
{
    promtView.hidden = YES;
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    [userInfo setValue:@"no" forKey:@"setlock"];
    [userInfo synchronize];
    
}


-(void)hiddenKeyboard
{
    [self resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
